﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAntLibrary;
using System.Xml;
using System.IO;

namespace MAntConsole.Utility
{
    public class IO
    {
        public static List<ZboziNetDirect> ReadXml(FileInfo file)
        {
            ZboziNetDirect zbozi;
            List<ZboziNetDirect> result = new List<ZboziNetDirect>();

            XmlDocument doc = new XmlDocument();
            XmlNode root = null;
            try { doc.Load(file.FullName); }
            catch (Exception e)
            {
                return result;
            }

            // pruchod vnorenymi uzly korenoveho elementu
            root = doc.DocumentElement;

            foreach (XmlNode lNode in root.ChildNodes)
            {
                zbozi = new ZboziNetDirect();
                zbozi.Obrazky = new List<Picture>();
                zbozi.atributy = new List<Attributes>();

                if (lNode.Name.Equals("product"))
                {
                    //ted jsem v uzlu SHOPITEM a zpracuju jednotlive hodnoty
                    XmlNodeList deti = lNode.ChildNodes;

                    foreach (XmlAttribute s in lNode.Attributes)
                    {
                        if (s.Name.Equals("avail")) { zbozi.Dostupnost = s.Value; }
                        else if (s.Name.Equals("priceOrigNet")) { zbozi.CenaBezDPH = s.Value; }
                        else if (s.Name.Equals("priceOrig")) { zbozi.CenaSDPH = s.Value; }
                    }

                    foreach (XmlNode d in deti)
                    {
                        if (d.Name.Equals("category")) { zbozi.Kategorie = d.InnerText; continue; }
                        if (d.Name.Equals("name")) { zbozi.JmenoVyrobku = d.InnerText; continue; }



                        if (d.Name.Equals("images"))
                        {
                            foreach (XmlNode x in d)
                            {
                                XmlAttributeCollection atr = x.Attributes;
                                Picture P = new Picture();
                                Uri uri;
                                foreach (XmlAttribute a in atr)
                                {
                                    if (a.Name.Equals("url")) { P.preview = a.Value; }
                                    else if (a.Name.Equals("origurl")) 
                                    { 
                                        P.fullImage = a.Value;
                                        uri = new Uri(a.Value);
                                        P.filename = System.IO.Path.GetFileName(uri.LocalPath);
                                    }
                                }
                                zbozi.Obrazky.Add(P);
                            }
                            continue;
                        }




                        if (d.Name.Equals("description"))
                        {
                            zbozi.Popis = d.InnerText;
                            continue;
                        }
                        if (d.Name.Equals("description_html")) { zbozi.KratkyPopis = d.InnerText; continue; }

                        if (d.Name.Equals("attributes"))
                        {
                            foreach (XmlNode nd in d.ChildNodes)
                            {
                                zbozi.atributy.Add(new Attributes(nd.Attributes[0].Value, nd.InnerText));
                            }
                            continue;
                        }
                    }
                }
                //MainWindow.NactenySeznam.Add(zbozi);
                result.Add(zbozi);
                // TMP
                //return result;
            }

            Console.WriteLine(string.Format("Nacteno zaznamu: {0}", result.Count));
            return result;
        }
    }
}
